//
//  WelcomeViewController.swift
//  基于Swift的微博设计与实现
//
//  Created by 张源海 on 15/11/27.
//  Copyright © 2015年 张源海. All rights reserved.
//

import UIKit
import SDWebImage
class WelcomeViewController: UIViewController {

    // 设置界面， 视图的层次结构 
    
    override func loadView() {
        // 直接使用背景图片做为根视图
        view = backImageView
        
        setupUI()
        
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // 异步加载用户头像
        iconView.sd_setImageWithURL(UserAccountViewModel.sharedUserAccount.avatarUrl , placeholderImage:UIImage(named: "avatar_default_big") )
        
    }
    // 设置动画  
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        iconView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(view.snp_bottom).offset(-view.bounds.height + 200)
        }
  // 动画
        welcomlabel.alpha = 0
        
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            // 修改所有可动画属性 的 自动布局
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(0.8, animations: { () -> Void in
                    self.welcomlabel.alpha = 1
                    },completion:{ (_) -> Void in
                        
//                        print("ok")
                        // 发送通知
                        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewControllerNotification, object: nil)
                        
                
                })
        }
        
    }
    
    

   
    
//  懒加载控件 
    // 背景图片
    private lazy var  backImageView: UIImageView = UIImageView(imageName: "ad_background")
    // 头像 
    
      lazy var iconView: UIImageView = {
        
        let iv = UIImageView(imageName: "avatar_default_big")
        
        // 设置圆角矩形 
        
        iv.layer.cornerRadius = 45
        iv.layer.masksToBounds = true
        
        
        return iv
    }()
/// 欢迎标签 
    private lazy var welcomlabel : UILabel = UILabel(title: "欢迎回来", fontSize:18 )
    
}

// MARK: - 设置界面 
extension WelcomeViewController {
    
    private func setupUI() {
        // 添加控件
        
        backImageView.addSubview(iconView)
        backImageView.addSubview(welcomlabel)
        
        //  自动布局 
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(backImageView.snp_centerX)
            make.bottom.equalTo(backImageView.snp_bottom).offset(-200)
            make.width.equalTo(90)
            make.height.equalTo(90)
        }
        welcomlabel.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
        
        
    }
    
    
    
}








